package com.atguigu.daijia.common.result;


import lombok.Data;

/**
 * 全局统一返回结果类
 *
 */
@Data
public class Result<T> {

    //返回码
    private Integer code;

    //返回消息
    private String message;

    //返回数据
    private T data;

    public Result(){}

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    // 操作成功
    public static<T> Result<T> ok(){
        return Result.ok(null);
    }
    public static<T> Result<T> ok(T data){
        return build( ResultCodeEnum.SUCCESS,data);
    }

    // 操作失败
    public static<T> Result<T> fail(){
        return Result.fail(null);
    }

    public static<T> Result<T> fail(T data){
        return build(ResultCodeEnum.FAIL,data);
    }

    public static<T> Result<T> fail(ResultCodeEnum resultCodeEnum,T data){
        return build(resultCodeEnum,data);
    }

    public static<T> Result<T> fail(int code, String msg, T data){
        return build(code, msg, data);
    }

    // 返回数据
    public static <T> Result<T> build(ResultCodeEnum resultCodeEnum,T data) {
        Result<T> result = new Result<T>();
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        if (data != null)
            result.setData(data);
        return result;
    }

    public static <T> Result<T> build(int code, String message, T data) {
        Result<T> result = new Result<T>();
        result.setCode(code);
        result.setMessage(message);
        if (data != null)
            result.setData(data);
        return result;
    }

}
